package info.androidhive.slidingmenu;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import mindshare.JSONParser;
import mindshare.LoadContent;
import mindshare.Login;
import mindshare.MainActivity;
import mindshare.SessionManager;
import mindshare.Signup;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import info.androidhive.slidingmenu.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Notificador  {
	SessionManager session;
	List<NameValuePair> param = new ArrayList<NameValuePair>();
	List<NameValuePair> paramNicknames = new ArrayList<NameValuePair>();
	JSONParser jParser = new JSONParser();
	Context c;
	public List<String> contentList= new ArrayList<String>();
	private List<String> nicknamesList= new ArrayList<String>();
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	private boolean enviarNotificaciones=false;
	
	
	public Notificador(Context c){
		this.c=c;
	}
	

	class sendNotification extends LoadContent{
		List<NameValuePair> paramTemp = new ArrayList<NameValuePair>();
		public sendNotification(Context context, List<NameValuePair> param, String data) {
			super(context, param, data);
			paramTemp = new ArrayList<NameValuePair>(param);
		}
		@Override
		protected String doInBackground(String... args) {
			String url="http://mindshare.comli.com/gcm/send_message.php";
			//String url="http://10.48.107.90:8888/gcm/send_message.php?gcm_regId="+gcm_regId;
			Log.d("DEBUGEANDO","URL "+url);
			JSONObject json = jParser.makeHttpRequest(url, paramTemp);
			/*
			try {
				pass = json.getString("result");
				Log.d("DEBUGEANDO","Result "+pass);
			} catch (JSONException e) {
				e.printStackTrace();
				
			}
			*/
			return null;
			
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			pDialog.dismiss();
	        
		}
		
	}
	
	

	

class GetRegisterID extends LoadContent{
		String mensaje;
		String regID;
		List<NameValuePair> paramTemp = new ArrayList<NameValuePair>();
		public GetRegisterID(Context context, List<NameValuePair> param, String data,String mensaje) {
			
			super(context, param, data);
			this.mensaje=mensaje;
			Log.d("DEBUGEANDO", "param----------->"+param);
			this.paramTemp=new ArrayList<NameValuePair>(param);
		}

		@Override
		protected String doInBackground(String... args) {
			Log.d("DEBUGEANDO", "param back----------->"+paramTemp);
			JSONObject json = jParser.makeHttpRequest("http://mindshare.comli.com/obtainRegisters.php", paramTemp);
			JSONArray registers;
			try {
				registers = json.getJSONArray(data);
				for (int i = 0; i < registers.length(); i++) {
					regID = registers.getString(i);
					
				}
			} catch (JSONException e) {
				e.printStackTrace();
				Log.d("DEBUGEANDO", "ERROR: "+e.getMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			Log.d("DEBUGEANDO", "regId----------->"+regID);
			param.clear();
			param.add(new BasicNameValuePair("gcm_regId", regID));
			param.add(new BasicNameValuePair("mensaje", mensaje));
			sendNotification sendNotification= new sendNotification(c, param, "result");
			sendNotification.execute();
			pDialog.dismiss();
		}
		
	}

class GetNicknames extends LoadContent{
	String mensaje;
	public GetNicknames(Context context, List<NameValuePair> paramNicknames, String data,String mensaje) {
		super(context, paramNicknames, data);
		Log.d("DEBUGEANDO", "obtenerNicknames 3.1 "+paramNicknames);
		this.mensaje=mensaje;
	}

	@Override
	protected String doInBackground(String... args) {
		Log.d("DEBUGEANDO", "obtenerNicknames 3.2 " +paramNicknames);
		JSONObject json = jParser.makeHttpRequest("http://mindshare.comli.com/obtainFriendsCategory.php", paramNicknames);
		JSONArray registers;
		try {
			Log.d("DEBUGEANDO", "obtenerNicknames 3.3");
			registers = json.getJSONArray(data);
			Log.d("DEBUGEANDO", "obtenerNicknames 3.4 "+registers.toString());
			for (int i = 0; i < registers.length(); i++) {
				nicknamesList.add(registers.getString(i));
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
			Log.d("DEBUGEANDO", "ERROR: "+e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String file_url) {
		Log.d("DEBUGEANDO", "obtenerNicknames 3.5");
		for (int i = 0; i < nicknamesList.size(); i++) {
			Log.d("DEBUGEANDO", ">>>>>>>>>>>>NICK: "+nicknamesList.get(i));
		}
		for (int i = 0; i < nicknamesList.size(); i++) {
			Log.d("DEBUGEANDO", "obtenidendo de "+nicknamesList.get(i));
			obtenerRegisterID(nicknamesList.get(i),mensaje);
		}
		pDialog.dismiss();
	}
	
}
	
public void obtenerRegisterID(String nickname,String mensaje){
	param.clear();
	Log.d("DEBUGEANDO", "En obtenerRetisterID");
	param.add(new BasicNameValuePair("nickname", nickname));
	GetRegisterID getRegisterID = new GetRegisterID(c, param, "register",mensaje);
	getRegisterID.execute();
	
	
			
}


public void obtenerNicknames(String nickname,String category,String mensaje){
	paramNicknames.clear();
	Log.d("DEBUGEANDO", "obtenerNicknames 1");
	paramNicknames.add(new BasicNameValuePair("nickname", nickname));
	paramNicknames.add(new BasicNameValuePair("category", category));
	Log.d("DEBUGEANDO", "obtenerNicknames 2");
	GetNicknames getnicknames = new GetNicknames(c, paramNicknames, "nicknames",mensaje);
	Log.d("DEBUGEANDO", "obtenerNicknames 3");
	getnicknames.execute();
	Log.d("DEBUGEANDO", "obtenerNicknames 4");
	
	
			
}



	public void notificar(String nickname,String categoria,String mensaje){
		param.clear();
		//El metodo debe darme el arrayList de nicknames que son amigos del nickname parametro y que conocen la categoria parametro
		obtenerNicknames(nickname, categoria,mensaje);
		
				
	}
	
	public void responder(String nicknamePreguntador,String respuesta){
		
		param.clear();
		Log.d("DEBUGEANDO", "En obtenerRetisterID");
		param.add(new BasicNameValuePair("nickname", nicknamePreguntador));
		GetRegisterID getRegisterID = new GetRegisterID(c, param, "register",respuesta);
		getRegisterID.execute();
		
	}
	

}
